<template>
  <div class="page">
    <el-form label-width="80px" inline :model="searchFrom">
      <el-form-item label="姓名">
        <el-input v-model="searchFrom.name"></el-input>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-select v-model="searchFrom.status" placeholder="请选择启用状态" clearable style="width:100%">
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="doctorQuery">搜索</el-button>
        <el-button @click="show()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border>
      <el-table-column
        prop="name"
        label="姓名"
        width="80">
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="科室"
        width="80">
      </el-table-column>
      <el-table-column
        prop="gender"
        label="性别"
        width="60">
      </el-table-column>
      <el-table-column
        prop="jobTitle"
        label="职称"
        width="150">
      </el-table-column>
      <el-table-column
        prop="status"
        label="启用状态"
        width="60">
        <template slot-scope="{row}">
          {{ row.status === '0' ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="introduction"
        label="描述"
        width="">
      </el-table-column>
      <el-table-column
        prop="expertise"
        label="专业技能"
        width="">
      </el-table-column>
      
      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="{row}">
          <el-button type="text" @click="show(row)">修改</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="deleteDoctor(row.id)"
          >
            <el-button slot="reference" type="text" style="color: #F56C6C;">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageNoChange"
      :current-page.sync="page.pageNo"
      :page-size="page.pageSize"
      layout="total, prev, pager, next"
      :total="page.total">
    </el-pagination>

    <el-dialog
      :title="form.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            :action="imagesUploadApi"
            :headers="{Authorization: 'Bearer ' + this.$store.getters.token}"
            :show-file-list="false"
            accept="image/*"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.storagePath" :src="baseApi + '/file/' + form.storagePath" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="科室">
          <el-select v-model="form.deptCode" placeholder="请选择科室" clearable style="width:100%" @change="changeDept">
            <el-option v-for="item in deptList" :key="item.id" :label="item.branchCode+'/' + item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="职称">
          <el-input v-model="form.jobTitle" placeholder="请输入职称"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender" placeholder="请选择性别" style="width:100%">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="启用状态">
          <el-select v-model="form.status" placeholder="请选择启用状态" style="width:100%">
            <el-option label="启用" value="0" />
            <el-option label="禁用" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="描述">
          <el-input type="textarea" v-model="form.introduction" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="专业技能">
          <el-input type="textarea" v-model="form.expertise" placeholder="请输入姓名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { doctorQuery, doctorUpdate, doctorAdd, getDepart, doctorDelete } from '@/api/official/official'
import { mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  data() {
    return {
      searchFrom: {},
      page:{
        total:0,
        pageSize:10,
        pageNo:1,
      },
      tableData: [],
      form: {},
      dialogVisible: false,
      loading: false,
      deptList: []
    }
  },
  created() {
    this.doctorQuery()
    this.getDepart()
  },
  methods: {
    getDepart() {
      getDepart({size: 999}).then(res => {
        this.deptList = res.content
      })
    },
    doctorQuery() {
      doctorQuery({...this.searchFrom, page: this.page.pageNo, size: this.page.pageSize}).then(res => {
        this.tableData = res.content
        this.page.total = res.totalElements
      })
    },
    pageSizeChange(val){
      this.page.pageSize = val;
      this.doctorQuery();
    },
    pageNoChange(val){
      this.page.pageNo = val;
      this.doctorQuery();
    },
    show(row = {}) {
      this.dialogVisible = true
      this.form = {...row}
    },
    handleAvatarSuccess(res, file) {
      // this.$set(this.form, 'storagePath', this.baseApi + '/file/' + res.data.path)
      this.$set(this.form, 'storagePath', res.data.path)
      this.$set(this.form, 'storageId', res.data.uuid)
    },
    beforeAvatarUpload(file) {
      const supportedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
      const isValidType = supportedTypes.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 50;

      if (!isValidType) {
        this.$message.error('上传封面只能是图片格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传封面图片大小不能超过 50MB!');
      }
      return isValidType && isLt2M;
    },
    changeDept(dept) {
      this.form.deptName = this.deptList.find(item => item.id === this.form.deptCode).name
      this.form.branchCode = this.deptList.find(item => item.id === this.form.deptCode).branchCode
    },
    submit() {
      this.loading = true
      const data = {
        ...this.form
      }
      if (this.form.id) {
        doctorUpdate(data).then(res => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.doctorQuery()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      } else {
        doctorAdd(data).then(res => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.doctorQuery()
        }).finally(() => {
          this.loading = false
          this.dialogVisible = false
        })
      }
    },
    deleteDoctor(id) {
      doctorDelete([id]).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.doctorQuery()
      })
    }
  }
}
</script>

<style scoped>
.page {
  padding: 24px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 0;
}
</style>
